package com.lfk.demo;

import com.lfk.justwetools.Utils.Log;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.IDialog;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Component;

import com.lfk.demo.Ability.CartoonAbility;
import com.lfk.demo.Ability.CodeAbility;
import com.lfk.demo.Ability.EditAbility;
import com.lfk.demo.Ability.ExplorerAbility;
import com.lfk.demo.Ability.NewPaintAbility;
import com.lfk.demo.Ability.PaintViewAbility;
import com.lfk.demo.Ability.ReadAbility;
import com.lfk.demo.Ability.VerTextAbility;
import com.lfk.demo.Ability.ViewDemoAbility;
import ohos.utils.net.Uri;

import java.io.*;

public class MainAbility extends Ability implements Component.ClickedListener {
    private LayoutScatter inflater;
    private final int BUFFER_SIZE = 400000;
    public static final String FILE_NAME = "codeview.java";
    public static final String READ_FILE_NAME = "exm.txt";
    public static final String PACKAGE_NAME = "com.lfk.justwetools";
    public static String FILE_PATH;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        FILE_PATH = getDataDir() + "/bin";
        Log.i("FILE_PATH: "+FILE_PATH);
        setUIContent(ResourceTable.Layout_ability_main);
        inflater = LayoutScatter.getInstance(this);
        findComponentById(ResourceTable.Id_paintview_button).setClickedListener(this);
        findComponentById(ResourceTable.Id_explorerview_button).setClickedListener(this);
        findComponentById(ResourceTable.Id_codeview_button).setClickedListener(this);
        findComponentById(ResourceTable.Id_readerview_button).setClickedListener(this);
        findComponentById(ResourceTable.Id_markdown_button).setClickedListener(this);
        findComponentById(ResourceTable.Id_vertextview).setClickedListener(this);
        findComponentById(ResourceTable.Id_newPaint).setClickedListener(this);
        findComponentById(ResourceTable.Id_viewDemo).setClickedListener(this);
        findComponentById(ResourceTable.Id_cartoonDemo).setClickedListener(this);
    }

    private void showAbout() {
        Component view = inflater.parse(ResourceTable.Layout_text_item, null, false);
        CommonDialog dialog = new CommonDialog(this);
        dialog.setTitleText("关于JustWeTools");
        dialog.setContentCustomComponent(view);
        dialog.setButton(IDialog.BUTTON3, "确定", new IDialog.ClickedListener() {
            @Override
            public void onClick(IDialog iDialog, int i) {
            }
        });
    }

    public void copytext(String name, int type) {
        try {
            File myDataPath = new File(FILE_PATH);
            if (!myDataPath.exists()) {
                myDataPath.mkdirs();
            }
            String file = myDataPath + "/" + name;
            if (!(new File(file).exists())) {
                InputStream is;
                if (type == 0) {
                    is = getResourceManager().getRawFileEntry("resources/rawfile/codeview.java").openRawFile();
                } else {
                    is = getResourceManager().getRawFileEntry("resources/rawfile/exm.txt").openRawFile();
                }
                FileOutputStream fos = new FileOutputStream(file);
                byte[] buffer = new byte[BUFFER_SIZE];
                int count = 0;
                while ((count = is.read(buffer)) > 0) {
                    fos.write(buffer, 0, count);
                }
                fos.close();
                is.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(Component v) {
        Intent intent = new Intent();
        Intent.OperationBuilder builder = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getBundleName());
        switch (v.getId()) {
            case ResourceTable.Id_paintview_button:
                builder.withAbilityName(PaintViewAbility.class);
                break;
            case ResourceTable.Id_explorerview_button:
                builder.withAbilityName(ExplorerAbility.class);
                break;
            case ResourceTable.Id_codeview_button:
                copytext(FILE_NAME, 0);
                builder.withUri(Uri.parse(FILE_PATH + "/" + FILE_NAME));
                builder.withAbilityName(CodeAbility.class);
                break;
            case ResourceTable.Id_readerview_button:
                copytext(READ_FILE_NAME, 1);
                builder.withUri(Uri.parse(FILE_PATH + "/" + READ_FILE_NAME));
                builder.withAbilityName(ReadAbility.class);
                break;
            case ResourceTable.Id_markdown_button:
                builder.withAbilityName(EditAbility.class);
                break;
            case ResourceTable.Id_vertextview:
                builder.withAbilityName(VerTextAbility.class);
                break;
            case ResourceTable.Id_newPaint:
                builder.withAbilityName(NewPaintAbility.class);
                break;
            case ResourceTable.Id_viewDemo:
                builder.withAbilityName(ViewDemoAbility.class);
                break;
            case ResourceTable.Id_cartoonDemo:
                builder.withAbilityName(CartoonAbility.class);
                break;
        }
        intent.setOperation(builder.build());
        startAbility(intent);
    }
}
